<template>
  <el-dialog
    v-if="dialogShow"
    v-model="dialogShow"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    append-to-body
    custom-class="mydialog"
    width="1000px"
    title="添加终端"
    @close="close"
  >
    <el-form
      ref="formRef"
      :model="addClientForm"
      :label-width="formLabelWidth"
      :inline="true"
      size="small"
      :rules="formRules"
    >
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item
            label="产品信息"
            style="width:100%"
          >
            <el-select
              v-model="addClientForm.productId"
              style="width: 100%"
              clearable
              filterable
              disabled
              placeholder="请选择产品信息"
            >
              <el-option
                v-for="item in productOptions"
                :key="item.id"
                :label="getProductName(item)"
                :value="item.id"
              >
                <div class="pro-item">
                  <span>{{ getProductName(item) }}</span>
                </div>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="所在地区"
            style="width:100%"
          >
            <el-cascader
              v-model="addClientForm.area"
              :popper-append-to-body="false"
              style="width: 100%;"
              :options="citys"
              placeholder="请选择所在地区"
              :props="{ expandTrigger: 'hover', label: 'regionName', value:'regionCode' }"
              collapse-tags
              clearable
              @change="areaChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="终端名称"
            style="width:100%"
          >
            <el-input
              v-model="addClientForm.terminalName"
              placeholder="请输入终端名称"
              @blur="terminalSearch"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      ref="multipleTable"
      header-cell-class-name="table-header"
      :data="clientListData"
      :border="true"
      style="width: 100%"
      max-height="400px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="55"
      />
      <el-table-column
        label="终端编号"
        prop="terminalNo"
        header-align="center"
        align="center"
      >
        <template #default="scope">{{ scope.row.terminalNo }}</template>
      </el-table-column>
      <el-table-column
        label="终端名称"
        prop="terminalName"
        header-align="center"
      />
      <el-table-column
        label="所在地区"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          <div>{{ scope.row.provinceName }} / {{ scope.row.cityName }} / {{ scope.row.areaName }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="产品信息"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          <div>{{ getProductName(scope.row) }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="终端价（元）"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          <div>{{ scope.row.terminalPrice }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="渠道客户名称"
        prop="agentName"
        header-align="center"
        align="center"
      />
      <el-table-column
        label="当前配送费"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          <div v-if="scope.row.computeMode==='固定价格'">
            <span>{{ scope.row.distributePrice }}元</span><span v-if="scope.row.sellUnit"> / {{ getLabel('sell_unit',scope.row.sellUnit) }}</span>
          </div>
          <div v-if="scope.row.computeMode==='终端价百分比'">
            <span>{{ scope.row.distributePrice }}%</span><span>终端价</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="配送服务编号"
        header-align="center"
      >
        <template #default="scope">
          <div>
            {{ scope.row.distributeServeNo }}
          </div>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <div
        class="dialog-footer"
        style="display:flex;justify-content:center;align-items:center;"
      >
        <el-button
          class="border-btn"
          size="small"
          @click="close"
        >取消</el-button>
        <el-button
          class="main-btn"
          size="small"
          type="primary"
          @click="confirm"
        >确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {defineComponent, inject, reactive, ref, toRefs} from 'vue'
import area from '@/utils/area.js'
import {crmStore} from '@CRM/store'
import {useRoute} from 'vue-router'

export default defineComponent({
  name: 'AddTerminal',
  setup(props, context) {
    const route = useRoute()
    const formRef = ref(null)
    const api = inject('api')
    const citys = area
    var reactiveData = reactive({
      terminalData: crmStore.getters['dictionary/getDictionary']['app_mode'],
      addClientForm: {
        productId: 0,
        distributeId: 0,
        distributeDetailId: 0,
        oldDistributeId: Number(route.query.id),
        area: [],
        areaCode: '',
        cityCode: '',
        provinceCode: '',
        terminalName: '',
      },
      clientListData: [],
      selectionList: [],
      productOptions: [],
    })
    var formRules = reactive({})
    var formLabelWidth = ref(90)
    var dialogShow = ref(false)
    var priceNoOptions = reactive([])

    const handleSelectionChange = (val) => {
      reactiveData.selectionList = val
    }
    const reset = () => {
      // formRef.value.resetFields()
      reactiveData.selectionList.splice(0, reactiveData.selectionList.length)
    }
    const open = (data) => {
      reactiveData.addClientForm.productId = data.productInfo.id
      reactiveData.addClientForm.distributeId = data.distributeId
      // 厂家配送
      if (data.distributeMode === '1') {
        reactiveData.addClientForm.distributeDetailId = 0
      } else {
        reactiveData.addClientForm.distributeDetailId =
          data.distributePriceInfo?.id || 0
      }
      getTerminalList(reactiveData.addClientForm)
      getDistributeProductList(data.distributeId)
      dialogShow.value = true
    }
    const close = () => {
      reset()
      dialogShow.value = false
    }
    // 获取该配送商下有效产品列表
    const getDistributeProductList = (distributeId) => {
      api.distributors
        .DISTRIBUTORS_PRODUCT_LIST({
          distributeId: distributeId,
        })
        .then((res) => {
          if (res.code === 0) {
            reactiveData.productOptions = res.data.list
          }
        })
    }
    const getTerminalList = (data) => {
      api.distributors
        .DISTRIBUTORS_RELATION_CHANGE_TERMINAL_LIST(data)
        .then((res) => {
          if (res.code === 0) {
            console.log(res.data)
            reactiveData.clientListData = res.data.list
          }
        })
    }
    const areaChange = (val) => {
      if (val && val.length) {
        reactiveData.addClientForm.provinceCode = val[0]
        reactiveData.addClientForm.cityCode = val[1]
        reactiveData.addClientForm.areaCode = val[2]
      } else {
        reactiveData.addClientForm.provinceCode = ''
        reactiveData.addClientForm.cityCode = ''
        reactiveData.addClientForm.areaCode = ''
      }
      getTerminalList(reactiveData.addClientForm)
    }
    const terminalSearch = () => {
      getTerminalList(reactiveData.addClientForm)
    }
    const confirm = () => {
      context.emit('selectSuccess', reactiveData.selectionList)
      dialogShow.value = false
      console.log(reactiveData.selectionList)
    }
    return {
      citys,
      formRef,
      ...toRefs(reactiveData),
      formRules,
      formLabelWidth,
      dialogShow,
      priceNoOptions,
      open,
      close,
      confirm,
      handleSelectionChange,
      terminalSearch,
      areaChange,
    }
  },
})
</script>
